﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineShop
{
    public class Shop : INameable
    {
        private string name;
        //private List<Product> products;
        public Stock stock;
        private decimal turnover;
        private string password;

        public string Password
        {
            get { return this.password; }
            set { this.password = value; }
        }
       
        public decimal Turnover
        {
            get
            { return this.turnover; }
            set
            {
                if (value<0)
                {
                    throw new ArgumentOutOfRangeException("Turnover must be greater or equal to 0.");
                }
                turnover = value; 
            }
        }

        //public List<Product> Products
        //{
        //    get { return products; }
        //    private set { products = value; }
        //}

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        //A method to adjusts the available stock and turnover after a sale.
        public void  MakeSale(Product product, decimal unitPrice, int numberItemsSold) 
            {
                //Check if the product is available.
                if (!this.stock.stockList.ContainsKey(product))
                {
                    throw new ArgumentOutOfRangeException("This product is not available in this shop.");
                }
                //numberItemsSold -number of itmes sold, amount - available items of product in stock.
                if (this.stock.stockList[product] - numberItemsSold >= 0)
                {
                    this.stock.stockList[product] -= numberItemsSold;
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Quantity in stock insufficient for this sale.");
                }
                decimal saleAmount;
                saleAmount = unitPrice* numberItemsSold;
                this.Turnover += saleAmount;
        }

        public void DisplayStock()
        {
            Console.WriteLine("Stock in store {0}", this.Name);
            Console.WriteLine(this.stock.TakeStock());
        }

        //Constructors
        public Shop(string name)
        {
            this.Name = name;
            this.stock = new Stock();
        }

        public Shop(string name, decimal turnover)
            :this(name)
        {
            this.Turnover = turnover;
        }
 

        //Override ToString
        public override string ToString()
        {
            StringBuilder result = new StringBuilder();
            result.AppendFormat("Store: {0}", this.Name);
            result.AppendFormat("\nDate: {0}", DateTime.Now);
            result.AppendFormat("\nCurrent Turnover: {0}$", this.Turnover);
            return result.ToString();
        }
    }
}
